{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import backtrader as bt  \n",
    "import pandas as pd\n",
    "from datetime import datetime\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import akshare as ak"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 大智慧m5m60导出txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取文本文件\n",
    "file_path = 'data/M5M60.txt'  # 替换为你的文件路径\n",
    "data_m5m60 = pd.read_csv(file_path, delimiter='\\t', header=None, names=['Date', 'Value'])\n",
    "# 将 'Date' 列转换为日期格式\n",
    "data_m5m60['Date'] = pd.to_datetime(data_m5m60['Date'])\n",
    "data_m5m60.columns = ['date', 'value',]\n",
    "# 日期列转换时间类型\n",
    "data_m5m60['date'] = pd.to_datetime(data_m5m60['date'])\n",
    "# 时间索引\n",
    "data_m5m60.index = pd.to_datetime(data_m5m60['date'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 截取数据\n",
    "# 定义开始和结束日期  \n",
    "start_date = '2023-05-01'  # 例如：2023年1月1日  \n",
    "end_date = datetime.now()   # 例如：2023年6月30日  \n",
    "\n",
    "# 将字符串转换为datetime对象（如果它们不是的话）  \n",
    "start_date = datetime.strptime(start_date, '%Y-%m-%d')  \n",
    "end_date = end_date.strftime(\"%Y-%m-%d\")\n",
    "\n",
    "# 创建布尔掩码\n",
    "mask = (data_m5m60['date'] >= start_date) & (data_m5m60['date'] <= end_date)\n",
    "data_m5m60=data_m5m60.loc[mask]\n",
    "# 打印 DataFrame\n",
    "print(data_m5m60.tail())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 大智慧自定义数据文件读入函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "def read_dzh_data(file_path):#大智慧自定义数据文件\n",
    "    \"\"\"\n",
    "    读取文本文件并将其转换为 DataFrame，同时将 'Date' 列转换为日期格式，并设置为索引。\n",
    "\n",
    "    参数:\n",
    "    file_path (str): 文本文件的路径。\n",
    "\n",
    "    返回:\n",
    "    pd.DataFrame: 处理后的 DataFrame。\n",
    "    \"\"\"\n",
    "    # 读取文本文件\n",
    "    data_dzh = pd.read_csv(file_path, delimiter='\\t', header=None, names=['Date', 'Value'])\n",
    "    \n",
    "    # 将 'Date' 列转换为日期格式\n",
    "    data_dzh['Date'] = pd.to_datetime(data_dzh['Date'])\n",
    "    \n",
    "    # 重命名列\n",
    "    data_dzh.columns = ['date', 'value']\n",
    "    \n",
    "    # 将 'date' 列转换为日期格式\n",
    "    data_dzh['date'] = pd.to_datetime(data_dzh['date'])\n",
    "    \n",
    "    # 设置 'date' 列为索引\n",
    "    data_dzh.index = pd.to_datetime(data_dzh['date'])\n",
    "    \n",
    "    return data_dzh\n",
    "\n",
    "# 示例调用\n",
    "file_path = 'data/M5M60.txt'  # 替换为你的文件路径\n",
    "data_dzh = read_dzh_data(file_path)\n",
    "print(data_dzh)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
